package com.foreveross.bsl.common.utils.balance.cxf;

import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.message.Exchange;

import java.util.List;

/**
 * Supports pluggable strategies for alternate endpoint selection on
 * load balance.
 * <p/>
 * Random, Retries, Mod (later)
 * <p/>
 * 
 * 1. support load balance  
 * 2.support fail over.
 *
 * @author Vector Ho   Date:2014-10-16 18:14
 * @see org.apache.cxf.clustering.FailoverStrategy
 */
public interface LoadBalanceStrategy {

    /**
     * Get the alternate endpoints for this invocation.
     *
     * @param exchange the current Exchange
     * @return a failover endpoint if one is available
     */
    List<Endpoint> getAlternateEndpoints(Exchange exchange);

    /**
     * Select one of the alternate endpoints for a retried invocation.
     *
     * @param alternates List of alternate endpoints if available
     * @return the selected endpoint
     */
    Endpoint selectAlternateEndpoint(List<Endpoint> alternates);

    /**
     * Get the alternate addresses for this invocation.
     * These addresses over-ride any addresses specified in the WSDL.
     *
     * @param exchange the current Exchange
     * @return a failover endpoint if one is available
     */
    List<String> getAlternateAddresses(Exchange exchange);

    /**
     * Select one of the alternate addresses for a retried invocation.
     *
     * @param addresses List of alternate addresses if available
     * @return the selected address
     */
    String selectAlternateAddress(List<String> addresses);

    /**
     * should remove failed endpoint or not.
     * only work for user defined addresses list.
     * @return true or false
     */
    boolean isRemoveFailedEndpoint();

    /**
     * change endpoint every time or not.
     * @return boolean
     */
    boolean isAlwaysChangeEndpoint();

    /**
     * remove failed address from list.
     * @param address the failed address
     */
    void removeAlternateAddress(String address);
}
